/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res=new ArrayList<Integer>();
        Deque<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return res;
        }
        TreeNode node=null;
        while(root!=null||!queue.isEmpty()){
            while(root!=null){
                queue.push(root);
                root=root.left;

            }
            root=queue.pop();
            if(root.right==null||root.right==node){
                res.add(root.val);
                node=root;
                root=null;
            }else{
                queue.push(root);
                root=root.right;
            }

        }
        return res;

    }
}
